<?php

namespace app\admin\controller;

use app\admin\common\Adminbase;
use service\FeishuService;
use service\GuardService;
use service\TreeService;
use app\admin\model\User as Usermodel;
use think\Cache;
use think\Db;

class User extends Adminbase
{
    protected $user_model;

    protected function _initialize(){
        parent::_initialize();
        $this->user_model = new Usermodel();
    }

    public function index(){
        if($this->request->isAjax()) {
            $param = $this->request->param();//接收参数
            /**
             * 返回获取的分页数据
             */
            return $this->user_model->GetUsersDataPage($param);
        }

        //获取集团总部部门
        $deparmentkey = "deparmentlists";
        $data = Cache::store('redis')->get($deparmentkey);
        if(empty($data)){
            $fields = "department_id as id,department_id,title,pid";
            $deparmentlists = Db::name("department")->field($fields)
                //->where(['is_jtzb'=>1])
                ->where(['status'=>1])->order("sort asc")->select();
            /*foreach ($deparmentlists as &$row){
                if($row['pid'] == 10000000){
                    $row['pid'] = 0;
                }
                //$row['spread'] = true;
            }*/
            //dump($deparmentlists);exit;
            $data = json_encode(TreeService::DeepTree($deparmentlists),true);
            Cache::store('redis')->set("deparmentlists",$data,600);
        }
        $this->assign("data", $data);

        return $this->fetch();
    }

    public function syncuser(){
        if($this->request->isAjax()) {
            $data = $this->request->param();
            //dump($data);exit;
            if(empty($data['user_id'])){
                $this->error("请先查询用户信息");
            }
            $userinfo = db('user')->where(['user_id'=>$data['user_id']])->find();
            $u_type = 'contact.user.created_v3';
            if(!empty($userinfo)){
                //$this->error("系统中存在此用户，无需同步");
                $u_type = 'contact.user.updated_v3';
            }
            FeishuService::instance()->AddEditUser($data['user_id'], $u_type);
            $this->success("同步用户信息成功");
        }
        return $this->fetch();
    }

    public function queryuserinfo(){
        $userinfo = $this->request->post("userinfo","","trim");
        if(!empty($userinfo)){
            $fsuserinfo = FeishuService::instance()->QueryUser($userinfo);
            //dump($fsuserinfo);exit;
            if($fsuserinfo['code'] == 200){
                $this->success("获取成功",null,$fsuserinfo['data']);
            }
            else{
                $this->error("没有此员工信息，请输入正确的员工工号");
            }
        }
    }

    public function setfloor(){
        if($this->request->isAjax()) {
            $data = $this->request->param();
            //dump($data);exit;
            $userinfo = Db::name("user")->where(['id'=>$data['id']])->find();
            if(!isset($data['floorinfo'])){
                $this->error("请选择电梯楼层");
            }
            $udata = [];
            $udata['floorinfo'] = implode(",",$data['floorinfo']);
            $udata['guardinfo'] = "";
            if(isset($data['guardinfo'])){
                $udata['guardinfo'] = implode(",",$data['guardinfo']);
            }
            $guarduserinfo = GuardService::instance()->getPersoninfo($userinfo['user_id']);
            if($guarduserinfo['code'] == 0){
                //用户存在
                $user = [
                    "user_id" => $userinfo['user_id'],
                    "guardinfo" => $udata['guardinfo']
                ];
                $res = GuardService::instance()->EditPersonLevel($user);
                if($res['code'] != 0 ){
                    $this->error($res['message']);
                }
            }
            else{
                //门禁系统用户不存在
                $user = [
                    "user_id" => $userinfo['user_id'],
                    "mobile" => $userinfo['mobile'],
                    "username" => $userinfo['username'],
                    "gender" => $userinfo['gender'],
                    "avatar_origin" => $userinfo['avatar_origin'],
                    "guardinfo" => $udata['guardinfo']
                ];
                if($userinfo['is_jtzb'] == 1){
                    $user['department_id'] = $userinfo['department_id'];
                }
                $res = GuardService::instance()->editUser($user);
                if($res['code'] != 0 ){
                    $this->error($res['message']);
                }
            }
            Db::name("user")->where(['id'=>$data['id']])->update($udata);
            $this->success("设置成功");
        }
        $id = $this->request->param("id/d");
        $info = Db::name("user")->where(['id'=>$id])->find();
        $info['floorinfo'] = explode(",",$info['floorinfo']);
        $userinfo = GuardService::instance()->getPersoninfo($info['user_id']);
        //dump($userinfo);exit;
        $info['guardinfo'] = ( isset($userinfo['code']) && $userinfo['code'] == 0 && isset($userinfo['data']) && is_array($userinfo['data']) && !empty($userinfo['data']) ) ? explode(",",$userinfo['data']['accLevelIds']) : [];
        $this->assign("info",$info);
        //dump($info);exit;
        $levellists = GuardService::instance()->Getacclevellists();
        $levellists = ( isset($levellists['code']) && $levellists['code'] == 0 && isset($levellists['data']) && is_array($levellists['data']) && !empty($levellists['data']) ) ? $levellists['data'] : [];
        //dump($levellists);exit;
        $this->assign("levellists",$levellists);
        return $this->fetch();
    }

}